package com.doublethread.easymart.modules.product.controller;

import com.doublethread.easymart.common.result.PageResult;
import com.doublethread.easymart.common.result.Result;
import com.doublethread.easymart.modules.product.dto.ProductQueryDTO;
import com.doublethread.easymart.modules.product.service.ProductService;
import com.doublethread.easymart.modules.product.vo.ProductVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/public/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 获取商品列表
     */
    @GetMapping
    public Result<PageResult<ProductVO>> getProductList(ProductQueryDTO queryDTO) {
        log.info("获取商品列表");

        PageResult<ProductVO> pageResult = productService.getProductList(queryDTO);

        log.info("获取商品列表成功");

        return Result.success(pageResult);
    }

    /**
     * 获取商品详情
     */
    @GetMapping("/{id}")
    public Result<ProductVO> getProductById(@PathVariable Long id) {
        log.info("获取商品详情");

        ProductVO productVO = productService.getProductById(id);

        log.info("获取商品详情成功");
        return Result.success(productVO);
    }

    /**
     * 搜索商品
     */
    @GetMapping("/search")
    public Result<PageResult<ProductVO>> searchProducts(ProductQueryDTO queryDTO) {
        log.info("搜索商品");

        PageResult<ProductVO> pageResult = productService.searchProducts(queryDTO);

        log.info("搜索商品成功");
        return Result.success(pageResult);
    }

    /**
     * 获取热门商品
     */
    @GetMapping("/hot")
    public Result<List<ProductVO>> getHotProducts(@RequestParam(defaultValue = "10") Integer limit) {
        log.info("获取热门商品");

        List<ProductVO> products = productService.getHotProducts(limit);

        log.info("获取热门商品成功");
        return Result.success(products);
    }

    /**
     * 获取新品推荐
     */
    @GetMapping("/new")
    public Result<List<ProductVO>> getNewProducts(@RequestParam(defaultValue = "10") Integer limit) {
        log.info("获取新品推荐");

        List<ProductVO> products = productService.getNewProducts(limit);

        log.info("获取新品推荐成功");
        return Result.success(products);
    }

    /**
     * 获取相关商品
     */
    @GetMapping("/{id}/related")
    public Result<List<ProductVO>> getRelatedProducts(@PathVariable Long id,
                                                      @RequestParam(defaultValue = "10") Integer limit) {
        log.info("获取相关商品");

        List<ProductVO> products = productService.getRelatedProducts(id, limit);

        log.info("获取相关商品成功");
        return Result.success(products);
    }

    /**
     * 获取品牌列表
     */
    @GetMapping("/brands")
    public Result<List<String>> getBrands() {
        log.info("获取品牌列表");

        List<String> brands = productService.getBrands();

        log.info("获取品牌列表成功");
        return Result.success(brands);
    }
}
